﻿#region License

// Another free, open solution by someone who gets it.
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#endregion

using System;

using FluentValidation;

namespace Patterns.Validation
{
	/// <summary>
	/// Provides extensions to all types to expedite validation in small use cases.
	/// </summary>
	public static class ValidationExtensions
	{
		/// <summary>
		/// Validates the specified instance.
		/// </summary>
		/// <typeparam name="T">The instance type.</typeparam>
		/// <param name="instance">The instance.</param>
		/// <param name="validationLogic">The validation logic.</param>
		/// <param name="throwOnFailure">if set to <c>true</c>, throws an exepction upon validation failure. the default is <c>true</c>.</param>
		/// <param name="target">The target.</param>
		/// <param name="message">The message.</param>
		/// <returns>
		///   <c>true</c> if the instance is valid; otherwise, <c>false</c>.
		/// </returns>
		public static bool QuickValidate<T>(this T instance, Predicate<T> validationLogic, bool throwOnFailure = true, string target = null, string message = null)
		{
			IValidator<T> validator = new LambdaValidator<T>(validationLogic, target, message);
			if(!throwOnFailure) return validator.Validate(instance).IsValid;
			validator.ValidateAndThrow(instance);
			return true;
		}
	}
}